package com.wilmer.ai.client;

import com.unfbx.chatgpt.OpenAiClient;
import com.unfbx.chatgpt.OpenAiStreamClient;
import com.wilmer.ai.config.AiConfig;
import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

/**
 * @author hans
 * 24425
 * 构建fastgpt客户端
 */
@Component
public class StreamClient {

    @Resource
    AiConfig aiConfig;


    @Bean
    public OpenAiStreamClient openAiStreamClient(){

         //代理ip，端口
        OkHttpClient okHttpClient = new OkHttpClient
                .Builder()
                //自定义超时时间
                .connectTimeout(60, TimeUnit.SECONDS)
                //自定义超时时间
                .writeTimeout(600, TimeUnit.SECONDS)
                //自定义超时时间
                .readTimeout(600, TimeUnit.SECONDS)
                .build();
        return OpenAiStreamClient.builder()
                .apiKey(Arrays.asList(aiConfig.getKeys()))
                //设置自定义的okHttpClient客户端
                .okHttpClient(okHttpClient)
                //自定义ApiHost
                .apiHost(aiConfig.getUrl())
                .build();
    }
    @Bean
    public OpenAiClient openAiClient(){
        //代理ip，端口
        OkHttpClient okHttpClient = new OkHttpClient
                .Builder()
                //自定义超时时间
                .connectTimeout(60, TimeUnit.SECONDS)
                //自定义超时时间
                .writeTimeout(600, TimeUnit.SECONDS)
                //自定义超时时间
                .readTimeout(600, TimeUnit.SECONDS)
                .build();
        return OpenAiClient.builder()
                .apiKey(Arrays.asList(aiConfig.getKeys()))
                //设置自定义的okHttpClient客户端
                .okHttpClient(okHttpClient)
                //自定义ApiHost
                .apiHost(aiConfig.getUrl())
                .build();
    }
}
